import datetime
import time

from flask import render_template, request, current_app, session, g, redirect, url_for, jsonify

from info import constants, db
from info.admin import admin_blue
from info.models import User, News, Category
from info.utils.common import user_login_data
from info.utils.image_storage import storage
from info.utils.response_code import RET


@admin_blue.route('/login', methods=['GET', 'POST'])
def admin_login():
    if request.method == 'GET':
        user_id = session.get('user_id', None)
        is_admin = session.get('is_admin', False)
        if user_id and is_admin:
            return redirect(url_for('admin.admin_index'))
        return render_template('admin/login.html')

    username = request.form.get("username")
    password = request.form.get("password")

    if not all([username, password]):
        return render_template('admin/login.html', errmsg='参数不足')

    try:
        user = User.query.filter(User.mobile == username).first()
    except Exception as e:
        current_app.logger.error(e)
        return render_template('admin/login.html', errmsg='数据查询失败')

    if not user:
        return render_template('admin/login.html', errmsg='用户不存在')

    if not user.check_password(password):
        return render_template('admin/login.html', errmsg='密码错误')

    if not user.is_admin:
        return render_template('admin/login.html', errmsg='用户权限错误')

    session['user_id'] = user.id
    session['nick_name'] = user.nick_name
    session['mobile'] = user.mobile
    session['is_admin'] = True

    return redirect(url_for('admin.admin_index'))


@admin_blue.route('/')
@user_login_data
def admin_index():
    user = g.user
    if not user:
        return redirect(url_for('admin.admin_login'))

    context = {
        'user': user.to_dict()
    }

    return render_template('admin/index.html', context=context)


@admin_blue.route('/user_count')
def user_count():
    total_count = 0
    try:
        total_count = User.query.filter(User.is_admin == False).count()
    except Exception as e:
        current_app.logger.error(e)

    now = time.localtime()
    mon_count = 0
    try:
        mon_begin = '%d-%02d-01' % (now.tm_year, now.tm_mon)
        mon_begin_date = datetime.datetime.strptime(mon_begin, '%Y-%m-%d')
        mon_count = User.query.filter(User.is_admin == False, User.create_time >= mon_begin_date).count()
    except Exception as e:
        current_app.logger.error(e)

    day_count = 0
    try:
        day_begin = '%d-%02d-%02d' % (now.tm_year, now.tm_mon, now.tm_mday)
        day_begin_date = datetime.datetime.strptime(day_begin, '%Y-%m-%d')
        day_count = User.query.filter(User.is_admin == False, User.create_time >= day_begin_date).count()
    except Exception as e:
        current_app.logger.error(e)

    now_date = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d'), '%Y-%m-%d')
    active_date = []
    active_count = []

    for i in range(31):
        begin_date = now_date - datetime.timedelta(days=i)
        end_date = now_date - datetime.timedelta(days=(i-1))
        active_date.append(begin_date.strftime('%Y-%m-%d'))
        count = 0

        try:
            count = User.query.filter(User.is_admin == False, User.last_login >= begin_date,
                                      User.last_login < end_date).count()
        except Exception as e:
            current_app.logger.error(e)

        active_count.append(count)

    active_count.reverse()
    active_date.reverse()

    data = {
        "total_count": total_count,
        "mon_count": mon_count,
        "day_count": day_count,
        "active_date": active_date,
        "active_count": active_count
    }

    return render_template('admin/user_count.html', data=data)


@admin_blue.route('/user_list')
def user_list():
    page = request.args.get('p', 1)
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1

    users = []
    current_page = 1
    total_page = 1
    filters = [
        User.is_admin == False,
        User.mobile.startswith('$') == False
    ]
    try:
        paginate = User.query.filter(
            *filters
        ).order_by(
            User.last_login.desc()
        ).paginate(
            page, constants.ADMIN_USER_PAGE_MAX_COUNT, False
        )
        users = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)

    users_list = []
    for user in users:
        users_list.append(user.to_admin_dict())

    data = {
        "total_page": total_page,
        "current_page": current_page,
        "users": users_list
    }

    return render_template('admin/user_list.html', data=data)


@admin_blue.route('/news_review')
def news_review():
    page = request.args.get('p', 1)
    keywords = request.args.get('keywords', '')
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1

    news_list = []
    current_page = 1
    total_page = 1

    filters = [
        (News.status != 0),
        (News.title.startswith('$$$isDeleted$$$') == False),
    ]
    if keywords:
        filters.append(News.title.contains(keywords))

    try:
        paginate = News.query.filter(
            *filters
        ).order_by(
            News.create_time.desc()
        ).paginate(
            page, constants.ADMIN_NEWS_PAGE_MAX_COUNT, False
        )

        news_list = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)

    news_dict_list = []
    for news in news_list:
        news_dict_list.append(news.to_review_dict())

    data = {
        'total_page': total_page,
        'current_page': current_page,
        'news_list': news_dict_list
    }

    return render_template('admin/news_review.html', data=data)


@admin_blue.route('/news_review_detail', methods=['GET', 'POST'])
def news_review_detail():
    if request.method == 'GET':
        news_id = request.args.get('news_id')
        if not news_id:
            return render_template('admin/news_review_detail.html', data={"errmsg": "未查询到此新闻"})

        news = None
        try:
            news = News.query.get(news_id)
        except Exception as e:
            current_app.logger.error(e)

        if not news:
            return render_template('admin/news_review_detail.html', data={"errmsg": "未查询到此新闻"})

        data = {
            "news": news.to_dict()
        }

        return render_template('admin/news_review_detail.html', data=data)

    news_id = request.json.get('news_id')
    action = request.json.get('action')

    if not all([news_id, action]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    if action not in['accept', 'reject']:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    print(news_id, action)

    news = None
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据库错误")

    if not news:
        return jsonify(errno=RET.NODATA, errmsg="未查询到数据")

    if action == 'accept':
        news.status = 0
    else:
        reason = request.json.get('reason')
        if not reason:
            return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
        news.reason = reason
        news.status = -1

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")

    print("操作成功")
    return jsonify(errno=RET.OK, errmsg="操作成功")


@admin_blue.route('/news_edit')
def news_edit():
    page = request.args.get('p', 1)
    keywords = request.args.get('keywords', '')
    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1

    news_list = []
    current_page = 1
    total_page = 1

    try:
        filters = [(News.title.startswith('$$$isDeleted$$$') == False)]
        if keywords:
            filters.append(News.title.contains(keywords))
        paginate = News.query.filter(
            *filters
        ).order_by(
            News.create_time.desc()
        ).paginate(
            page, constants.ADMIN_NEWS_PAGE_MAX_COUNT, False
        )
        news_list = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)

    news_dict_list = []
    for news in news_list:
        news_dict_list.append(news.to_basic_dict())

    data = {
        "total_page": total_page,
        "current_page": current_page,
        "news_list": news_dict_list
    }

    return render_template('admin/news_edit.html', data=data)


@admin_blue.route('/news_edit_detail', methods=['GET', 'POST'])
def news_edit_detail():
    if request.method == 'GET':
        news_id = request.args.get('news_id')
        if not news_id:
            return render_template('admin/news_edit_detail.html', data={"errmsg": "未查询到此新闻"})

        news = None

        try:
            news = News.query.get(news_id)
        except Exception as e:
            current_app.logger.error(e)
            return render_template('admin/news_edit_detail.html', data={"errmsg": "未查询到此新闻"})

        if not news:
            return render_template('admin/news_edit_detail.html', data={"errmsg": "未查询到此新闻"})

        categories = Category.query.all()
        categories_li = []
        for category in categories:
            c_dict = category.to_dict()
            c_dict['is_selected'] = False
            if category.id == news.category_id:
                c_dict['is_selected'] = True
            categories_li.append(c_dict)
        categories_li.pop(0)

        data = {
            "news": news.to_dict(),
            "categories": categories_li
        }

        return render_template('admin/news_edit_detail.html', data=data)

    news_id = request.form.get('news_id')
    title = request.form.get('title')
    digest = request.form.get('digest')
    content = request.form.get('content')
    index_image = request.form.get('index_image')
    category_id = request.form.get('category_id')

    if not all([title, digest, content, category_id]):
        print('title=', title)
        print('digest=', digest)
        print('content=', content)
        print('category_id=', category_id)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    news = None
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
    if not news:
        return jsonify(errno=RET.NODATA, errmsg="未查询到数据")

    if index_image:
        try:
            index_image = index_image.read()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

        try:
            key = storage(index_image)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.THIRDERR, errmsg="上传图片错误")
        news.index_image_url = constants.QINIU_DOMIN_PREFIX + key

    news.title = title
    news.digest = digest
    news.content = content
    news.category_id = category_id

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")

    return jsonify(errno=RET.OK, errmsg="编辑成功")


@admin_blue.route('/news_category', methods=['GET'])
def get_news_category():
    categories = Category.query.all()
    categories_dicts = [category.to_dict() for category in categories]
    categories_dicts.pop(0)
    return render_template('admin/news_type.html', data={"categories": categories_dicts})


@admin_blue.route('/add_category', methods=['POST'])
def add_category():

    category_id = request.json.get('id')
    category_name = request.json.get('name')

    if not category_name:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    if category_id:
        try:
            category = Category.query.get(category_id)
            category.name = category_name
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg="查询数据失败")

        if not category:
            return jsonify(errno=RET.NODATA, errmsg="未查询到分类信息")

    else:
        category = Category()
        category.name = category_name
        db.session.add(category)

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")

    return jsonify(errno=RET.OK, errmsg="保存数据成功")


@admin_blue.route('/delete_user', methods=['POST'])
def delete_user():

    user_id = request.json.get('user_id')

    if not user_id:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    user = None
    try:
        user = User.query.get(user_id)
    except Exception as e:
        current_app.logger.error(e)

    if not user:
        return jsonify(errno=RET.DBERR, errmsg="用户id错误")

    # 因正常注册的用户名皆为手机号，而数据库字段长度设为了11，采取替换首字符法，既能标记也能保留删除数据
    user.mobile = '$' + user.mobile[1:]

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")

    return jsonify(errno=RET.OK, errmsg="成功删除用户")


@admin_blue.route('/delete_news', methods=['POST'])
def delete_news():

    news_id = request.json.get('news_id')

    if not news_id:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    news = None
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)

    if not news:
        return jsonify(errno=RET.DBERR, errmsg="新闻id错误")

    news.title = '$$$isDeleted$$$' + news.title

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")

    return jsonify(errno=RET.OK, errmsg="成功删除新闻")
